<script lang="ts" setup>
import { computed, reactive, ref, watch } from "vue"
import { getApi } from "@/api/erpOrders"
import { Search, Refresh, RefreshRight } from "@element-plus/icons-vue"
import { type FormInstance } from "element-plus"
import { usePagination } from "@/hooks/usePagination"
import ImportFile from "@/components/ImportFile/index.vue"
import dayjs from "dayjs"

defineOptions({
  // 命名当前组件
  name: "ErpOrders"
})
const searchData = reactive({
  order_number: "",
  store_name: "",
  erp_data_date: "",
  erp_order_number: ""
})
const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 查
const tableData = ref<any[]>([])
const params = reactive({
  erp_data_date: "",
  file: ""
})
const getTableData = () => {
  loading.value = true
  if (searchData.erp_data_date) {
    searchData.erp_data_date = dayjs(searchData.erp_data_date).format("YYYY-MM-DD")
  }
  getApi({
    page: paginationData.page,
    size: paginationData.size,
    ...searchData
    // erp_data_date: params.erp_data_date
  })
    .then(({ data }: any) => {
      paginationData.total = data.pagination.total
      tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}

//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.page, () => paginationData.size], getTableData, { immediate: true })
const importRef = ref()
const formRef = ref()
const formRules = {
  file: [{ required: true, trigger: "blur", message: "请上传文件" }],
  erp_data_date: [{ required: true, trigger: "blur", message: "请选择数据日期" }]
}

const submit = async () => {
  formRef.value
    ?.validate(async (valid: boolean, fields: any) => {
      if (!valid) return console.error("表单校验不通过", fields)
      loading.value = true
      await importRef.value.submitUpload()
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSuccess = () => {
  getTableData()
}
const importData = computed(() => {
  return {
    erp_data_date: dayjs(params.erp_data_date).format("YYYY-MM-DD")
  }
})
const searchFormRef = ref<FormInstance | null>(null)

const handleSearch = () => {
  paginationData.page === 1 ? getTableData() : (paginationData.page = 1)
}

const resetSearch = () => {
  searchFormRef.value?.resetFields()
  handleSearch()
}
</script>

<template>
  <div class="app-container">
    <el-card shadow="never" class="search-wrapper">
      <el-collapse accordion>
        <el-collapse-item name="1">
          <template #title>导入数据</template>
          <div>
            <el-form ref="formRef" :model="params" :rules="formRules">
              <el-form-item prop="erp_data_date" label="数据日期" required>
                <el-date-picker v-model="params.erp_data_date" placeholder="请选择" style="width: 400px" />
              </el-form-item>
              <el-form-item prop="file" label="" required>
                <div>
                  <ImportFile
                    path="/api/erpOrders/import"
                    v-model="params.file"
                    ref="importRef"
                    :data="importData"
                    :handleSuccess="handleSuccess"
                  />
                </div>
              </el-form-item>
              <el-form-item>
                <div>
                  <el-button type="primary" @click="submit">提交</el-button>
                </div>
              </el-form-item>
            </el-form>
          </div>
        </el-collapse-item>
      </el-collapse>
    </el-card>
    <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="order_number" label="订单编号">
          <el-input v-model="searchData.order_number" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="erp_order_number" label="ERP订单编号">
          <el-input v-model="searchData.erp_order_number" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="store_name" label="店铺名称">
          <el-input v-model="searchData.store_name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="erp_data_date" label="数据日期">
          <el-date-picker v-model="searchData.erp_data_date" placeholder="请选择" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="erp_order_number" label="ERP订单编号" fixed />
          <el-table-column prop="order_number" label="订单编号" fixed />
          <el-table-column prop="cost" label="邮资成本" />
          <el-table-column prop="store_name" label="店铺名称" />
          <el-table-column prop="erp_data_date" label="数据日期">
            <template #default="scope">
              {{ scope.row.erp_data_date ? dayjs(scope.row.erp_data_date).format("YYYY-MM-DD") : "" }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.size"
          :currentPage="paginationData.page"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
